import csv

import pandas as pd

if __name__ == '__main__':
    import requests


    def get_coordinates_by_baidumap(address, ak):
        url = f"http://api.map.baidu.com/geocoding/v3/?address={address}&output=json&ak={ak}"
        response = requests.get(url)
        data = response.json()
        if data['status'] == 0:
            location = data['result']['location']
            return location['lng'], location['lat']  # 返回经度，纬度
        else:
            return None, None


    # 使用示例
    ak = "FgcyhjOYj7iD7AYlbOG1YjToSTbyy9NI"
    print(get_coordinates_by_baidumap("中国北京北京朝阳区-酒仙桥街道-驼房营南路-首信小区", ak))
    # df = pd.read_csv("D:\家宽\综资\综资数据(更新至2025年4月29日)\全量小区.csv", encoding='gbk')
    # with open('/fttr/全量经纬度.csv', 'a', encoding='utf-8', newline='') as file:
    #     writer = csv.writer(file)
    #     for row in df.itertuples():
    #         address = row.中文名称
    #         lng, lat = get_coordinates_by_baidumap(address, ak)
    #         writer.writerow([address, lng, lat])  # 写入所有行
